<template>
  <div class="department-container">
    <div class="app-container aetting">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <!-- 新增角色按钮 -->
            <el-row style="height: 50px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="newAdd"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格  -->
            <el-table :data="tableData" border style="width: 100%">
              <el-table-column label="序号" width="150">
                <template v-slot="{ $index }">
                  <span>{{
                    $index + (pageParams.page - 1) * pageParams.pagesize + 1
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="角色名称" prop="name" width="150" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作" width="250">
                <template v-slot="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="hAssign(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="hight: 60px"
            >
              <el-pagination
                background
                :current-page="pageParams.page"
                :page-size="pageParams.pagesize"
                layout="prev,pager,next"
                :total="total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane class="gongsi" label="公司信息" name="second">
            <div style="width: 550px">
              <el-form ref="form" disabled label-width="150px">
                <el-form-item label="企业名称">
                  <el-input :value="companyList.name" />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input :value="companyList.companyAddress" />
                </el-form-item>
                <el-form-item label="公司电话">
                  <el-input :value="companyList.companyPhone" />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input :value="companyList.mailbox" />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input :value="companyList.remarks" />
                </el-form-item>
              </el-form>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹出框 -->
      <!-- @close是弹层关闭的事件 当showDialog从true->false就会执行close -->
      <el-dialog
        :title="ieEdit ? '新增角色' : '编辑角色'"
        :visible.sync="dialogVisible"
        width="50%"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          class="roleform"
          :model="roleForm"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <center>
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="hSubmit">确 定</el-button>
          </center>
        </span>
      </el-dialog>
    </div>
    <!-- 分配权限弹出层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assignPermission
        v-if="showDialogAssign"
        :role-id="roleId"
        @close="showDialogAssign = false"
      />
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoles,
  deleteRole,
  addRole,
  updateRole,
  getCompany
} from '@/api/employees.js'
import assignPermission from './assignPermission.vue'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      activeName: 'first',
      tableData: [],
      companyList: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 10 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      ieEdit: false, // 是否是编辑
      showDialogAssign: false, // 分配权限
      roleId: ''
    }
  },
  computed: {
    // 表格中最大的页码
    MaxPag() {
      return Math.ceil(this.total / this.pageParams.pagesize)
    },
    // 最后一页是不是满的
    isLastPageFulled() {
      return this.total % this.pageParams.pagesize === 0
    }
  },
  created() {
    this.loadRoles()
    this.getCompanyList()
  },
  methods: {
    // 获取公司信息
    async getCompanyList() {
      try {
        const id = this.$store.state.user.userInfo.companyId
        const { data } = await getCompany(id)
        this.companyList = data
      } catch (error) {
        console.log(error)
      }
    },
    // 初始化数据
    async loadRoles() {
      try {
        const { data } = await getRoles(this.pageParams)
        this.tableData = data.rows
        this.total = data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 监听页面变化的值
    currentChange(curpage) {
      this.pageParams.page = curpage
      // 重新发起请求
      this.loadRoles()
    },
    // 删除数据
    async doDel(id) {
      try {
        await deleteRole(id)
        this.$message.success('删除成功')
        // 如果当前表格只要一条，删除之后自动请求上一页的数据
        if (this.tableData.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        // 重新发起请求
        this.loadRoles()
      } catch (error) {
        this.$message.eror('删除失败')
      }
    },
    // 提示用户是否删除
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除
          this.doDel(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增角色
    newAdd() {
      this.ieEdit = true
      this.dialogVisible = !this.dialogVisible
    },
    // 添加角色
    async hoAdd() {
      try {
        await addRole(this.roleForm)
        this.$message.success('添加成功')
        // 重新发起请求
        this.loadRoles()
        // 关闭弹出层
        this.dialogVisible = false
        if (this.isLastPageFulled) {
          this.pageParams.page = this.MaxPag + 1
          this.total++
        } else {
          this.pageParams.page = this.MaxPag
        }
      } catch (error) {
        this.$message.error('添加失败')
      }
    },
    // 用户点击了保存
    hSubmit() {
      this.$refs.roleForm.validate(vali => {
        if (vali) {
          this.ieEdit ? this.hoAdd() : this.doEdit()
        }
      })
    },
    // 封装更新数据
    async doEdit() {
      try {
        await updateRole(this.roleForm)
        this.$message.success('更新成功')
        // 重新加载数据
        this.loadRoles()
        // 关闭弹出层
        this.dialogVisible = false
      } catch (error) {
        this.$message.error('更新失败')
      }
    },
    // 编辑角色
    hEdit(from) {
      // 当前为编辑状态
      this.ieEdit = false
      this.roleForm = { ...from }
      // 打开弹出层
      this.dialogVisible = true
    },
    // 重置表单
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 分配权限
    hAssign(id) {
      this.roleId = id
      // 显示弹出层
      this.showDialogAssign = true
    }
  }
}
</script>

<style lang="scss" scoped>
.aetting {
  padding: 15px !important;
  .el-tab-pane {
    padding: 30px;
  }
  .roleform {
    margin-top: 20px;
  }
  .gongsi {
    padding: 0;
  }
  ::v-deep.el-tabs__active-bar {
    width: 122px !important;
  }
  ::v-deep.el-tabs__item {
    width: 122px !important;
    padding: 0;
    text-align: center;
  }
  ::v-deep.el-form {
    padding: 30px 20px;
  }
  ::v-deep.el-dialog__body {
    padding: 0 !important;
  }
}
</style>
